package getfour.gui;
import getfour.game.Player;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class EndGame extends javax.swing.JDialog implements ActionListener {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -5651545433028536880L;
	private JPanel pnlMain;
	private JButton btnSave;
	private JButton btnAbort;
	private JComboBox cmbWinner;
	private JLabel lblWinner;
	private Boolean abort = false;
	private JFrame parent;
	
	public EndGame(JFrame frame) {
		super(frame);
		parent = frame;
		initGUI();
	}
	
	private void initGUI() {
		try {
			{
				this.setTitle("Das Spiel ist beendet");
			}
			{
				pnlMain = new JPanel();
				getContentPane().add(pnlMain, BorderLayout.CENTER);
				pnlMain.setLayout(null);
				pnlMain.setPreferredSize(new java.awt.Dimension(283, 97));
				{
					btnSave = new JButton();
					pnlMain.add(btnSave);
					btnSave.setText("speichern");
					btnSave.setBounds(190, 54, 81, 31);
					btnSave.addActionListener(this);
				}
				{
					lblWinner = new JLabel();
					pnlMain.add(lblWinner);
					lblWinner.setText("Gewinner:");
					lblWinner.setBounds(7, 12, 55, 14);
				}
				{
					ComboBoxModel cmbWinnerModel = 
						new DefaultComboBoxModel(
								new String[] { "Spieler X", "Spieler O" });
					cmbWinner = new JComboBox();
					pnlMain.add(cmbWinner);
					cmbWinner.setModel(cmbWinnerModel);
					cmbWinner.setBounds(123, 6, 148, 26);
				}
				{
					btnAbort = new JButton();
					pnlMain.add(btnAbort);
					btnAbort.setText("abbrechen");
					btnAbort.addActionListener(this);
					btnAbort.setBounds(7, 54, 81, 31);
				}
			}
			pack();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		
		if (source == btnAbort) {
			setVisible(false);
			((Game)parent).endgameHandler(this, true);
			dispose();
		}
		else if (source == btnSave) {
			((Game)parent).endgameHandler(this, false);
			setVisible(false);
			dispose();
		}
	}
	
	
	public void setWinningPlayer(Player player) {
		if (player == Player.X)
			cmbWinner.setSelectedIndex(0);
		else
			cmbWinner.setSelectedIndex(1);
	}

	public Player getWinningPlayer() {
		if (cmbWinner.getSelectedIndex() == 0)
			return Player.X;
		else
			return Player.O;
	}
}
